import org.w3c.dom.NodeList;
//自定义无头双向单链表
public class LinkedList {
    public NodeList head;
    public NodeList last;
    static class NodeList{
        public int val;
        public NodeList next;

        public NodeList prev;

        public NodeList(int val) {
            this.val = val;
        }

    }
    //头插法
    public void addFirst(int data){
        NodeList node = new NodeList(data);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }
    //尾插法
    public void addLast(int data){
       NodeList node = new NodeList(data);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        NodeList cur  = head;
        while(cur != null){
            cur = cur.next;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }
    //下标异常
    public void expecction(int index) throws IndexExpection{
        if(index < 0 || index > size()){
            throw new IndexExpection("下标错误");
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        NodeList node = new NodeList(data);
        int len = size();
        try {
            expecction(index);

            if (index == 0){
                addFirst(data);
                return true;
            }
            if (index == len) {
                addLast(data);
                return true;
            }
            //找下标
            NodeList cur = head;
            while (index > 0){
                cur = cur.next;
                index--;
            }
            node.prev = cur.prev;
            cur.prev.next = node;
            node.next = cur;
            cur.prev = node;

        }catch (Exception e){
            e.printStackTrace();
            System.out.println("接收到IndexExpection异常");
        }
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        NodeList cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;

    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return;
        }
        //在头部
        if (head.val == key){
            head = head.next;
            if (head != null){
                head.prev = null;
            }
            return;
        }
        //在尾部
        if (last.val == key){
            last.prev.next = null;
            last = last.prev;
        }
        //在中间
        NodeList cur = head;
        while (cur != null){
            if (cur.val == key){
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }

        NodeList cur = head;
        while (cur != null){
            //在头部
            if (head.val == key){
                head = head.next;
                if (head != null){
                    head.prev = null;
                }
            }
            else {
                //在尾部
                if (last.val == key){
                    last.prev.next = null;
                    last = last.prev;
                }
                //在中间
                if (cur.val == key){
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }

            }
            cur = cur.next;


        }
    }
    //得到单链表的长度
    public int size(){
        NodeList cur = head;
        int count = 0;
        while (cur != null){
           count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        NodeList cur = head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        NodeList cur = head.next;
        while (head != last){
            head.next = null;
            head.prev = null;
            head = cur;
            cur = cur.next;
        }
        head.prev = null;
        head = last = null;
    }
}
